package com.example.yrdev.ordering_system.Report;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Model.CardRecord;
import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.util.ArrayList;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by luke on 9/21/16.
 */
public class MemberReportDialog extends Dialog {
    Context context;
    int reportType;
     public Date startDate;
    public Date endDate;

    public MemberReportDialog(Context context, int reportType){
        super(context);
        this.context=context;
        this.reportType=reportType;

        requestWindowFeature(Window.FEATURE_NO_TITLE);

    }



    public MemberReportDialog(Context context, int reportType,ArrayList arrayList){

        super(context);
        this.context=context;
        this.reportType=reportType;
        requestWindowFeature(Window.FEATURE_NO_TITLE);

    }



    interface  Callback{
        void close();

    }
    public Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @InjectView(R.id.member_report_list)ListView member_report_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.member_report_layout);
        ButterKnife.inject(this);
        final ArrayList< CardRecord> records= DingHanDB.getInstance(context).getCardReport("",reportType);


        GlobalFunction.printText("tag","123asdnsad");
        GlobalFunction.printText("tag",String.valueOf(records));
        GlobalFunction.printText("tag",String.valueOf(records.size()));

        ReportMemberActivity reportMemberActivity=(ReportMemberActivity)context;
        try {

            member_report_adapter report_adapter=new member_report_adapter(context,records);
            member_report_list.setAdapter(report_adapter);

            //  DingHanDB.filterArrayBaseOnDate(records,startDate,endDate);
                    if (records.size()==0)
                    {
                         close();

                    }else {

                    }




        }catch (Exception e) {

         GlobalFunction.printTextByTag(e.getMessage());
        }






    }
        public void close(){
            if (this.callback!=null){
                this.callback.close();
                GlobalFunction.ToastAlert(context,"没有查到结果");
            }





        }



    @OnClick(R.id.close)
    public void closeView(){

        dismiss();

    }

    public class member_report_adapter extends BaseAdapter{
        Context context;
        ArrayList<CardRecord>arrayList;
        LayoutInflater layoutInflater;

        public member_report_adapter(Context context,ArrayList< CardRecord>records){

            this.context=context;
            arrayList=records;
            layoutInflater=LayoutInflater.from(context);

           //    layoutInflater=

        }
        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return arrayList.get(position);

        }

        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            CardRecord cardRecord=arrayList.get(position);
            ViewHolder viewHolder;

            if (convertView==null){
                convertView=layoutInflater.inflate(R.layout.member_report_row, null);
                View respView=convertView;
                viewHolder=new ViewHolder();

                 viewHolder.card_no_text=(TextView)respView.findViewById(R.id.card_no);
                 viewHolder.card_usageCount_text=(TextView)respView.findViewById(R.id.card_usage_count);
                 viewHolder.card_purchase_text=(TextView)respView.findViewById(R.id.card_purchase_amount);
                 viewHolder.card_remain_text=(TextView)respView.findViewById(R.id.card_remain);
                 viewHolder.card_discountRate_text =(TextView)respView.findViewById(R.id.card_discount_rate);
                viewHolder. card_date_text=(TextView)respView.findViewById(R.id.card_date);
                viewHolder.card_deviceNo_text=(TextView)respView.findViewById(R.id.card_deviceNo);
                viewHolder.card_Operator_text=(TextView)respView.findViewById(R.id.card_operator);
                viewHolder.card_recordType_text=(TextView)respView.findViewById(R.id.card_recordType);

                convertView.setTag(viewHolder);

            }else{
                viewHolder=(ViewHolder)convertView.getTag();

            }
            viewHolder.card_no_text.setText(cardRecord.getCardNo());
            viewHolder. card_usageCount_text.setText(String.valueOf(cardRecord.getUsageCount()));
            viewHolder. card_purchase_text.setText(String .valueOf(cardRecord.getPurchaseAmount())+" 元");
            viewHolder.card_remain_text.setText(String.valueOf(cardRecord.getRemain())+" 元");
            viewHolder.    card_discountRate_text.setText(cardRecord.getDiscountRate());
            viewHolder.  card_date_text.setText(cardRecord.getUsageDate());
            viewHolder. card_deviceNo_text.setText(cardRecord.getDeviceNo());
            viewHolder. card_Operator_text.setText(cardRecord.getOperator());
            viewHolder.  card_recordType_text.setText(String.valueOf(cardRecord.getReportType()));

            return convertView;

        }
    }
    static class ViewHolder{
        private TextView card_no_text;
        private TextView card_usageCount_text;
        private  TextView card_purchase_text;
        private TextView card_remain_text;
        private  TextView card_discountRate_text;
        private TextView card_date_text;
        private  TextView card_deviceNo_text;
        private   TextView card_Operator_text;
        private TextView card_recordType_text;

    }
}
